set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_User_Modify]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@n4UserNo	int
,	@strUserID	varchar(50)	= NULL
,	@IsLocalUser	char(1)	= NULL	
,	@strMD5PWD	varchar(100)	= NULL	
,	@strUserName	nvarchar(100)	= NULL	
,	@strPersonalNo	varchar(20)	= NULL	
,	@strEmail	varchar(100)	= NULL	
,	@n1Question	tinyint	= NULL	
,	@strAnswer	nvarchar(200)	= NULL	
,	@strPostNo	varchar(10)	= NULL	
,	@strAddress1	nvarchar(400)	= NULL	
,	@strAddress2	nvarchar(400)	= NULL	
,	@strHomePhone	varchar(20)	= NULL	
,	@strMobilePhone	varchar(20)	= NULL	
,	@strBirthday	varchar(20)	= NULL	
,	@IsLunar	char(1)		= NULL
,	@IsSMS	char(1)	= NULL	
,	@IsEmail	char(1)	= NULL	
,	@n1JoinMotiveType	tinyint	= NULL	
,	@n1UserLevel	tinyint	= NULL	
,	@n1GroupType	tinyint	= NULL	
,	@CodeSex	tinyint	= NULL	
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

-- Implementation


	UPDATE
		dbo.TB_User
	SET
		strUserID = IsNull( @strUserID, strUserID )
	,	IsLocalUser = IsNull( @IsLocalUser, IsLocalUser )
	,	strMD5PWD = IsNull( @strMD5PWD, strMD5PWD )
	,	strUserName = IsNull( @strUserName, strUserName )
	,	strPersonalNo = IsNull( @strPersonalNo, strPersonalNo )
	,	strEmail = IsNull( @strEmail, strEmail )
	,	n1Question = IsNull( @n1Question, n1Question )
	,	strAnswer = IsNull( @strAnswer, strAnswer )
	,	strPostNo = IsNull( @strPostNo, strPostNo )
	,	strAddress1 = IsNull( @strAddress1, strAddress1 )
	,	strAddress2 = IsNull( @strAddress2, strAddress2 )
	,	strHomePhone = IsNull( @strHomePhone, strHomePhone )
	,	strMobilePhone = IsNull( @strMobilePhone, strMobilePhone )
	,	strBirthday = IsNull( @strBirthday, strBirthday )
	,	IsLunar = IsNull( @IsLunar, IsLunar )
	,	IsSMS = IsNull( @IsSMS, IsSMS )
	,	IsEmail = IsNull( @IsEmail, IsEmail )
	,	n1JoinMotiveType = IsNull( @n1JoinMotiveType, n1JoinMotiveType )
	,	n1UserLevel = IsNull( @n1UserLevel, n1UserLevel )
	,	n1GroupType = IsNull( @n1GroupType, n1GroupType )
	,	CodeSex = IsNull( @CodeSex, CodeSex )
	,	dtModifyDate = @FrkDateNow
	WHERE
		n4UserNo = @n4UserNo


-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
